package com.cly.config;

import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * 静态配置
 * <p>
 * 用于配置静态映射
 *
 * @author 春风能解释
 * 2023/7/17
 */
@Configuration
@Slf4j
@RequiredArgsConstructor
public class WebMvcConfigurer extends WebMvcConfigurationSupport {

    /**
     * 自定义消息转换器
     */
    private final FastJsonHttpMessageConverter fastJsonHttpMessageConverter;

    /**
     * 静态资源映射
     *
     * @param registry 静态资源配置对象
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
    }

    /**
     * 自定义消息转换器，覆盖默认
     *
     * @param messageConverters 消息解析器
     */
    // https://blog.csdn.net/WithCYwind/article/details/129683009
    // https://blog.csdn.net/qq_36022463/article/details/122325576
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
        // 清理默认
        messageConverters.clear();
        messageConverters.add(0, fastJsonHttpMessageConverter);
    }
}
